package com.rao.gmall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rao.gmall.common.result.Result;
import com.rao.gmall.model.product.SpuImage;
import com.rao.gmall.model.product.SpuInfo;
import com.rao.gmall.model.product.SpuSaleAttr;
import com.rao.gmall.product.service.SpuImageService;
import com.rao.gmall.product.service.SpuInfoService;
import com.rao.gmall.product.service.SpuSaleAttrService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author nzmxfan
 * @create 2021-08-05-16:09
 */
@RestController
@RequestMapping("/admin/product")
public class SpuInfoController {

    @Resource
    SpuInfoService spuInfoService;

    @Resource
    SpuImageService spuImageService;

    @Resource
    SpuSaleAttrService spuSaleAttrService;

    /**
     * 分页查询SpuInfo
     * @param category3Id
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result<Page<SpuInfo>> getPageList(@PathVariable("page") Long page,
                              @PathVariable("limit") Long limit,
                              Long category3Id){

        Page<SpuInfo> infoPage = new Page<>(page, limit);

        QueryWrapper<SpuInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id", category3Id);

        Page<SpuInfo> spuInfoPage = spuInfoService.page(infoPage, queryWrapper);
        return Result.ok(spuInfoPage);
    }

    /**
     * 保存SPU
     * 1、spu_info
     * 2、spu_image ： spu把后来所有sku要用的图片全部上传好，录入sku的时候sku自己选
     * 3、spu_sale_attr、spu_sale_attr_value
     *  先操作spu_sale_attr，保存以下值
     *      "baseSaleAttrId":"1",
     *      "saleAttrName":"选择颜色",
     *  再操作spu_sale_attr_value
     *      spuSaleAttrValueList:[
     *          {
     *              "baseSaleAttrId":"1",
     *              "saleAttrValueName":"黑色"
     *              }
     *      ]
     *   注意：spu_sale_attr、spu_sale_attr_value有冗余存储项，
     *   需要提前设置好值
     *  http://api.gmall.com/admin/product/saveSpuInfo
     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){

        spuInfoService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    /**
     * 查询SPU对应的图片列表
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result<List<SpuImage>> spuImageList(@PathVariable("spuId") Long spuId){
        List<SpuImage> spuImageList = spuImageService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }

    /**
     * 根据spuId获取商品销售属性列表
     *    每一组销售属性的组合，应该对应一个SKU
     *  颜色：黄、蓝
     *  内存：128,256
     *  SKU： 2*2=4：
     *   黄+128  黄+256
     *   蓝+128  蓝+256
     *   我们在录入sku的时候，只需要录入我们的组合
     *
     * @param spuId
     * @return
     */
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> spuSaleAttrList(@PathVariable("spuId") Long spuId){

        List<SpuSaleAttr> spuSaleAttrList = spuSaleAttrService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }
}
